# coding:utf-8
from .data_collector import DataCollector
from bs4 import BeautifulSoup
import pandas as pd


class AppstoreCrawler(DataCollector):
    # pay
    app_type = "free"
    app_items = []
    
    def __init__(self):
        DataCollector.__init__(self)
        self.algorithm_name = "最热APP应用"
        self.ipynb_template_name = "appstore_crawler-template.ipynb"
    
    def implent(self):
        DataCollector.implent(self)
        self.app_items = []
        result = self.http.request(method="GET", url="https://www.applehunt.com/top")
        soup = BeautifulSoup(result.data.decode(), "html.parser")
        for item in soup.find_all("td", attrs={"class":self.app_type}):
            app_name = item.find("h4").text
            app_class = item.find("p").text.replace("分类：", "")
            app_pic_url = item.find("img")["data-original"]
            self.app_items.append([app_name, app_class, app_pic_url])
        self.df = pd.DataFrame(self.app_items, columns=["应用", "类别", "应用图片URL地址"])
    
    def prepareIpynbItems(self):
        DataCollector.prepareIpynbItems(self)
        self.ipynb_items["#app_type#"] = self.app_type

